package com.flower.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.flower.pojo.User;
import com.flower.service.IOrderService;
import com.flower.service.IUserService;

@Controller
public class PayController {
	
	@Resource(name = "userService")
	private IUserService iUserService;
	@Resource(name = "orderService")
	private IOrderService iOrderService;
	
	@RequestMapping("/payProduct.do")
	public String payProduct(HttpServletRequest request, int subTotal, int pid, String result){
		System.out.println(result);
		User user = (User) request.getSession().getAttribute("USER");
		if(user.getCount() - subTotal >= 0){
			int balance = user.getCount() - subTotal;
			iUserService.updateCount(balance, user.getUid());
			iOrderService.updateOstate(user.getUid(), 1, pid, result);
			request.getSession().setAttribute("balance", balance);
			request.getSession().setAttribute("ONUM", result);
		}else{
			request.getSession().setAttribute("balance", "购买的账单总额超出账户余额，请先去充值");
		}
		
		return "redirect:/payment.jsp";
	}
	
	@RequestMapping("/updateCount.do")
	@ResponseBody
	public String updateCount(HttpServletRequest request, int count, int balance, int onum){
		User user = (User) request.getSession().getAttribute("USER");
		int row = iUserService.updateCount(balance + count, user.getUid());
		if(row > 0){
			return "OK";
		}else{
			return "FAIL";
		}
	}
	
}
